'use strict';
import Base from './base.js';
// 美易通用户类类控制器请求方法
module.exports = class extends Base {
  // 请求获取用户信息
  async getuserinfoAction() {
    const user_id = this.get('user_id');
    const info = await this.model().query(' SELECT au.name,op.name as jobname,au.score,au.restday,au.headid,au.headurl,orz.name as zhichengname,au.username as tel FROM `think_or_app_user` au LEFT JOIN `think_or_position` op ON au.positionid = op.id LEFT JOIN `think_or_position` orz ON au.zhichengid = orz.id WHERE au.id = ' + user_id + ' ');
    if (think.isEmpty(info)) {
      return this.json({ 'code': '200', 'msg': '查询错误...', 'data': info });
    } else {
      return this.json({ 'code': '100', 'msg': 'success...', 'data': info });
    }
  }
  // 请求获取当前用户的出勤情况
  async getuserworkAction() {
    const user_id = this.get('user_id');
    // 获取今日开始的时间戳
    const time_start = Math.round(new Date(new Date().toDateString()).getTime() / 1000);
    // 今天结束时间
    const time_end = time_start + 86400;
    // 查询数据库
    const info = await this.model('or_user_work').where({ userid: user_id, time_start: { '>=': time_start }, time_end: { '<': time_end } }).find();
    // 返回查询结果
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查找成功...', 'data': info });
  }
  // 填写员工报表
  async getreportformAction() {
    const model = this.model('or_user_work');
    const user_id = this.post('user_id');
    const reportform = this.post('reportforms');
    // 获取今日开始的时间戳
    const time_start = Math.round(new Date(new Date().toDateString()).getTime() / 1000);
    // 今天结束时间
    const time_end = time_start + 86400;
    // 查询数据库
    const info = await model.where({ userid: user_id, time_start: { '>=': time_start }, time_end: { '<': time_end } }).update({ reportforms: reportform });
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '提交成功...', 'data': info });
  }
  // 请求更新当前用户头像
  async postuserheadAction() {
    const head_info = this.post();
    const info = await this.model('or_app_user').where({ id: head_info.user_id }).update({ headid: head_info.headid, headurl: head_info.headurl });
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查找成功...', 'data': info });
  }
  // 请求选择我服务的客户
  async getusermyfuwuguestAction() {
    const user_id = this.get('user_id');
    const res = this.model().query('SELECT * FROM `think_or_allotuser` oat LEFT JOIN `think_or_guest` og ON oat.gid = og.id WHERE oat.uid = ' + user_id + ' ');
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查找成功...', 'data': res });
  }
  // 获取我的日程信息
  async getmymanagedayAction() {
    const user_id = this.get('user_id');
    const year = this.get('year');
    const month = this.get('month');
    const day = this.get('day');
    const info = await this.model().query(' SELECT * FROM `think_or_user_manage_day` WHERE year = ' + year + ' AND month = ' + month + ' AND day = ' + day + '  AND userid = ' + user_id + ' ');
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '未找到日程...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 加入特殊日期到我的日程中
  async postaddspecialdaymanageAction() {
    const year = this.post('year');
    const month = this.post('month');
    const day = this.post('day');
    const user_id = this.post('user_id');
    const remark = this.post('remark');
    const info = await this.model('or_user_manage_day').add({ 'year': year, 'month': month, 'day': day, 'userid': user_id, 'remark': remark, 'type': 1 });
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '执行失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '添加成功...', 'data': info });
  }
  // 删除我的日程
  async postdelmanagedayAction() {
    const id = this.post('id');
    const res = await this.model('or_user_manage_day').where({ id: id }).delete();
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '删除失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': '删除成功...', 'data': '' });
  }
  // 获取当前用户下的下属员工
  async getmyworkerAction() {
    const floor = this.get('floor');
    const jsonshop = this.get('jsonshop');
    const departmentid = this.get('departmentid');
    const ismanage = this.get('ismanage');
    const workername = this.get('workername');
    switch (floor) {
      case '0':
        var info = await this.model().query(' SELECT au.id,au.name,IFNULL(od.name,"总部") as shopname,op.name as positionname,op1.name as zhichengname FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.shopid = od.id LEFT JOIN `think_or_position` op ON au.positionid = op.id LEFT JOIN `think_or_position` op1 ON au.zhichengid = op1.id WHERE au.isdisable = 0 AND (au.shopid in (' + jsonshop + ') or au.departmentid = ' + departmentid + ' ) AND (au.name LIKE "%' + workername + '%")');
        break;
      case '1':
        var info = await this.model().query(' SELECT au.id,au.name,IFNULL(od.name,"总部") as shopname,op.name as positionname,op1.name as zhichengname FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.shopid = od.id LEFT JOIN `think_or_position` op ON au.positionid = op.id LEFT JOIN `think_or_position` op1 ON au.zhichengid = op1.id WHERE au.shopid = ' + jsonshop + ' AND au.isdisable = 0 AND (au.name LIKE "%' + workername + '%")');
        break;
      case '2':
        var info = await this.model().query(' SELECT au.id,au.name,IFNULL(od.name,"总部") as shopname,op.name as positionname,op1.name as zhichengname FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.shopid = od.id LEFT JOIN `think_or_position` op ON au.positionid = op.id LEFT JOIN `think_or_position` op1 ON au.zhichengid = op1.id WHERE au.bumenid = ' + jsonshop + ' AND au.isdisable = 0 AND (au.name LIKE "%' + workername + '%")');
        break;
      case '3':
        if (ismanage == 1) {
          var info = await this.model().query(' SELECT au.id,au.name,IFNULL(od.name,"总部") as shopname,op.name as positionname,op1.name as zhichengname FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.shopid = od.id LEFT JOIN `think_or_position` op ON au.positionid = op.id LEFT JOIN `think_or_position` op1 ON au.zhichengid = op1.id WHERE au.xiaozuid = ' + jsonshop + ' And au.isdisable = 0 AND (au.name LIKE "%' + workername + '%")');
        }
        break;
    }
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 获取员工页面数据
  async getuserdataAction() {
    const user_id = this.get('user_id');
    // 获取今日开始的时间戳
    const starttime = Math.round(new Date(new Date().toDateString()).getTime() / 1000);
    // 今天结束时间
    const endtime = starttime + 86400;
    // 获取本月开始的时间戳
    var time_start_month = Math.round(new Date(new Date().getFullYear(), new Date().getMonth(), 1).getTime() / 1000);
    // 本月结束时间
    var time_end_month = Math.round(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 1).getTime() / 1000);
    // 获取今年开始的时间戳
    var time_start_year = Math.round(new Date(new Date().getFullYear(), 0, 1).getTime() / 1000);
    // 今年结束时间
    var time_end_year = Math.round(new Date(new Date().getFullYear() + 1, 0, 1).getTime() / 1000);
    // 定义api获取地址
    const datapath = think.config('mypath');
    const getapi = this.controller(datapath + '/database');
    // 获取员工信息
    const userinfo = await this.model().query(' SELECT org.jsonshop,od.floor,au.ismanage,au.departmentid FROM `think_or_app_user` au LEFT JOIN `think_or_department` od ON au.departmentid = od.id LEFT JOIN `think_or_rulegroup` org ON au.zhichengid = org.groupid WHERE au.id = ' + user_id + ' ');
    const floor = userinfo[0].floor.toString();
    const ismanage = userinfo[0].ismanage;
    if (floor == 0) {
      if (userinfo[0].jsonshop == '') { var jsonshop = 0 } else {
        var jsonshop = userinfo[0].jsonshop;
      }
    } else {
      var jsonshop = userinfo[0].departmentid;
    }
    // 今日出勤状态
    const res1 = await this.model().query('SELECT type FROM `think_or_user_work` WHERE time_start >= ' + starttime + ' AND time_start < ' + endtime + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res1)) { var todaywork = 0 } else { var todaywork = res1[0].type }
    // 本月出勤天数
    const res2 = await this.model().query('SELECT count(id) as num FROM `think_or_user_work` WHERE time_start >= ' + time_start_month + ' AND time_start < ' + time_end_month + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res2)) { var monthworknum = 0 } else { var monthworknum = res2[0].num }
    // 年度出勤天数
    const res3 = await this.model().query('SELECT count(id) as num FROM `think_or_user_work` WHERE time_start >= ' + time_start_year + ' AND time_start < ' + time_end_year + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res3)) { var yearworknum = 0 } else { var yearworknum = res3[0].num }
    // 今日工资
    const res4 = await this.model().query('SELECT IFNULL(sum(money),0) as num FROM `think_or_shop_userbasepay` WHERE time >= ' + starttime + ' AND time < ' + endtime + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res3)) { var todaymoney = 0 } else { var todaymoney = res4[0].num }
    // 本月工资
    const res5 = await this.model().query('SELECT IFNULL(sum(money),0) as num FROM `think_or_shop_userbasepay` WHERE time >= ' + time_start_month + ' AND time < ' + time_end_month + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res5)) { var monthmoney = 0 } else { var monthmoney = res5[0].num }
    // 年度工资
    const res6 = await this.model().query('SELECT IFNULL(sum(money),0) as num FROM `think_or_shop_userbasepay` WHERE time >= ' + time_start_year + ' AND time < ' + time_end_year + ' AND userid = ' + user_id + ' ');
    if (think.isEmpty(res6)) { var yearmoney = 0 } else { var yearmoney = res6[0].num }
    // 获取今日现金业绩
    const todayxianjin = await getapi.getyejixianjin(starttime, endtime, floor, jsonshop, ismanage, user_id);// 实际现金业绩
    // 获取本月现金业绩
    const monthxianjin = await getapi.getyejixianjin(time_start_month, time_end_month, floor, jsonshop, ismanage, user_id);// 实际现金业绩
    // 获取年度现金业绩
    const yearxianjin = await getapi.getyejixianjin(time_start_year, time_end_year, floor, jsonshop, ismanage, user_id);// 实际现金业绩
    // 获取今日规划业绩
    const todayplan = await getapi.getyejiplanxianjin(starttime, endtime, floor, jsonshop, ismanage, user_id);// 规划业绩
    // 获取本月规划业绩
    const monthplan = await getapi.getyejiplanxianjin(time_start_month, time_end_month, floor, jsonshop, ismanage, user_id);// 规划业绩
    // 获取年度规划业绩
    const yearplan = await getapi.getyejiplanxianjin(time_start_year, time_end_year, floor, jsonshop, ismanage, user_id);// 规划业绩

    // let kahao = await getapi.getyejikahao(starttime,endtime,floor,jsonshop,ismanage,user_id);//卡耗业绩

    return this.json({ 'code': '100', 'msg': '没有数据...', 'data': { 'floor': floor, 'jsonshop': JSON.parse(JSON.stringify(jsonshop)), 'ismanage': ismanage, 'todaywork': todaywork, 'monthworknum': monthworknum, 'yearworknum': yearworknum, 'todaymoney': todaymoney, 'monthmoney': monthmoney, 'yearmoney': yearmoney, 'todayxianjin': todayxianjin, 'monthxianjin': monthxianjin, 'yearxianjin': yearxianjin, 'todayplan': todayplan, 'monthplan': monthplan, 'yearplan': yearplan } });
  }

  // 获取用户当月的出勤记录
  async getusermonthworkinfoAction() {
    const user_id = this.get('user_id');
    const start_time = this.get('start_time');
    const end_time = this.get('end_time');
    const res = await this.model().query(' SELECT * FROM `think_or_user_work` WHERE userid = ' + user_id + ' AND time_start >= ' + start_time + ' AND time_start < ' + end_time + ' ');
    return think.isEmpty(res) ? this.json({ 'code': '200', 'msg': '本月没有出勤数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': res });
  }
  // 获取员工月份工资明细
  async getusermoneylistAction() {
    const user_id = this.get('user_id');
    const start_time_js = this.get('start_time_js');
    const end_time_js = this.get('end_time_js');
    // 查询提成
    const info = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 2 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');
    // 查询基本工资
    const info1 = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 1 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');
    // 指标工资
    const info2 = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 3 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');
    // 奖金工资
    const info3 = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 4 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');
    // 福利工资
    const info4 = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 5 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');
    // 扣除工资
    const info5 = await this.model().query(' SELECT type,sum(money) as money FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = 6 AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' GROUP BY type ');

    return this.json({ 'code': '100', 'msg': '查询成功...', 'sale': info, 'base': info1, 'zhibiao': info2, 'jiangjin': info3, 'fuli': info4, 'kouchu': info5 });
  }
  // 获取员工提成工资的列表
  async getusermoneysaleinfoAction() {
    const user_id = this.get('user_id');
    const start_time_js = this.get('start_time_js');
    const end_time_js = this.get('end_time_js');
    const category = this.get('category');
    const type = this.get('type');

    const info = await this.model().query(' SELECT osu.addtime as paytime,osu.orderid,og.name,osu.money,og.headurl,oso.addtime as ordertime,osu.remarks FROM `think_or_shop_userbasepay` osu LEFT JOIN `think_or_shop_order` oso ON osu.orderid = oso.id LEFT JOIN `think_or_guest` og ON oso.guestid = og.id WHERE osu.userid = ' + user_id + ' AND osu.category = ' + category + ' AND osu.type = ' + type + ' AND osu.addtime >= ' + start_time_js + ' AND osu.addtime < ' + end_time_js + ' ');

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查询成功...', 'data': info });
  }
  // 获取员工基本工资的列表
  async getusermoneyinfoAction() {
    const user_id = this.get('user_id');
    const start_time_js = this.get('start_time_js');
    const end_time_js = this.get('end_time_js');
    const category = this.get('category');
    const type = this.get('type');

    const info = await this.model().query(' SELECT addtime,money,remarks FROM `think_or_shop_userbasepay` WHERE userid = ' + user_id + ' AND category = ' + category + ' AND type = ' + type + ' AND time >= ' + start_time_js + ' AND time < ' + end_time_js + ' ');

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查询成功...', 'data': info });
  }
  // 获取今日员工的考勤状态
  async getuserkaoqinlistAction() {
    const departmentid = this.get('departmentid');
    const floor = this.get('floor');
    const start_time_js = this.get('start_time_js');
    const end_time_js = this.get('end_time_js');

    switch (floor) {
      case '0':
      case '1':
        var info = await this.model().query(' SELECT au.id,IFNULL(A.type,0) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + start_time_js + ' AND ouw.time_start < ' + end_time_js + ' AND au.shopid in (' + departmentid + ')) as A ON au.id = A.id WHERE au.shopid in (' + departmentid + ') ');
        break;
      case '2':
        var info = await this.model().query(' SELECT au.id,IFNULL(A.type,0) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + start_time_js + ' AND ouw.time_start < ' + end_time_js + ' AND au.bumenid = ' + departmentid + ' ) as A ON au.id = A.id WHERE au.bumenid = ' + departmentid + ' ');
        break;
      case '3':
        var info = await this.model().query(' SELECT au.id,IFNULL(A.type,0) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + start_time_js + ' AND ouw.time_start < ' + end_time_js + ' AND au.xiaozuid = ' + departmentid + ' ) as A ON au.id = A.id WHERE au.xiaozuid = ' + departmentid + ' ');
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 考勤审核时获取员工当日的考勤记录
  async getuserthisdayworkinfoAction() {
    const user_id = this.get('user_id');
    const start_time_js = this.get('start_time_js');
    const end_time_js = this.get('end_time_js');
    const info = await this.model().query(' SELECT id,name,headurl FROM `think_or_app_user` WHERE id = ' + user_id + ' ');
    const info1 = await this.model().query(' SELECT * FROM `think_or_user_work` WHERE userid = ' + user_id + ' AND time_start >= ' + start_time_js + ' AND time_start < ' + end_time_js + ' ');
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'user': info, 'work': info1 });
  }
  // 审核用户考勤
  async postusershenheworkAction() {
    const user_id = this.post('user_id');
    const start_time_js = this.post('start_time_js');
    const type = this.post('type');
    const add_money = this.post('add_money');
    const del_money = this.post('del_money');
    const remark = this.post('remark');
    const date = this.post('date');
    const name = this.post('name');
    const workid = this.post('workid');
    const add_rest = this.post('add_rest');
    const del_rest = this.post('del_rest');
    const addtime = Math.round(Date.now() / 1000);
    const model = this.model('or_user_work');
    try {
      await model.startTrans();
      var info = 0; var info1 = 0;
      if (type == 0) {
        let jilu = '';
        if (del_money != 0) { jilu = '扣除工资' + del_money + '元！' }
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: 0, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_jilu: jilu, shenhe_time: addtime, shenhe_name: name });
        } else {
          info = await model.where({ id: workid }).update({ type: type, shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark, shenhe_jilu: jilu });
        }
        const remarks = date + '扣除缺勤工资！';
        if (del_money != 0) {
          info1 = await this.model('or_shop_userbasepay').db(model.db()).add({ userid: user_id, category: 6, type: 1, money: -del_money, remarks: remarks, orderid: 0, addtime: addtime, time: addtime });
        } else { info1 = 1 }
      }

      if (type == 4) {
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: 4, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_time: addtime, shenhe_name: name });
          info1 = 1;
        } else {
          info = await model.where({ id: workid }).update({ type: type, shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark });
          info1 = 1;
        }
      }
      if (type == 7) {
        const jilu = '增加调休天数' + add_rest + '天！,加班费' + add_money + '元';
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: type, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_jilu: jilu, shenhe_time: addtime, shenhe_name: name });
        } else {
          info = await model.where({ id: workid }).update({ type: type, shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark, shenhe_jilu: jilu });
        }
        const remarks = date + '加班费！';
        var res1 = await this.model('or_app_user').db(model.db()).where({ id: user_id }).increment('restday', add_rest * 2);
        if (add_money != 0) {
          var res2 = await this.model('or_shop_userbasepay').db(model.db()).add({ userid: user_id, category: 4, type: 3, money: add_money, remarks: remarks, orderid: 0, addtime: addtime, time: addtime });
        } else { var res2 = 1 }
        if (!think.isEmpty(res1) && !think.isEmpty(res2)) { info1 = 1 } else { info1 = 0 }
      }
      if (type == 2) {
        let jilu = '';
        if (del_money != 0) { jilu = '扣除工资' + del_money + '元！' }
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: type, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_jilu: jilu, shenhe_time: addtime, shenhe_name: name });
        } else {
          info = await model.where({ id: workid }).update({ shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark, shenhe_jilu: jilu });
        }
        const remarks = date + '扣除请假工资！';
        if (del_money != 0) {
          info1 = await this.model('or_shop_userbasepay').db(model.db()).add({ userid: user_id, category: 6, type: 2, money: -del_money, remarks: remarks, orderid: 0, addtime: addtime, time: addtime });
        } else { info1 = 1 }
      }
      if (type == 3) {
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: type, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_time: addtime, shenhe_name: name });
          info1 = 1;
        } else {
          info = await model.where({ id: workid }).update({ type: type, shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark });
          info1 = 1;
        }
      }
      if (type == 5) {
        const jilu = '减少调休天数' + del_rest + '天！';
        if (think.isEmpty(workid)) {
          info = await model.add({ userid: user_id, type: type, time_start: start_time_js, shenhe: 1, shenhe_type: type, remark: remark, shenhe_jilu: jilu, shenhe_time: addtime, shenhe_name: name });
        } else {
          info = await model.where({ id: workid }).update({ type: type, shenhe: 1, shenhe_type: type, shenhe_name: name, shenhe_time: addtime, remark: remark, shenhe_jilu: jilu });
        }
        info1 = await this.model('or_app_user').db(model.db()).where({ id: user_id }).decrement('restday', del_rest * 2);
      }
      console.log(info); console.log('--------'); console.log(info1);
      if (!think.isEmpty(info) && !think.isEmpty(info1)) {
        await model.commit();
      } else {
        await model.rollback();
      }
    } catch (e) {
      console.log(e);
      await model.rollback();
    }

    if (think.isEmpty(info) || think.isEmpty(info1)) { return this.json({ 'code': '200', 'msg': '审核失败...', 'data': '' }) } else { return this.json({ 'code': '100', 'msg': 'success...', 'data': '' }) }
  }
  // 获取今日上班员工
  async gettodayworkAction() {
    const jsonshop = this.get('jsonshop');
    const floor = this.get('floor');
    const departmentid = this.get('departmentid');
    const starttime = this.get('start_time_js');
    const endtime = this.get('end_time_js');
    const datatype = this.get('datatype');
    console.log(datatype);
    // console.log(starttime);
    // //获取今日开始的时间戳
    // let time_start = Math.round(new Date(new Date().toDateString()).getTime()/1000);
    // //今天结束时间
    // let time_end = time_start + 86400;
    let info;
    switch (floor) {
      case '0':
        if (datatype == 1) {
          info = await this.model().query(' SELECT au.id,IFNULL(A.type,9) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe,au.isdisable FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + starttime + ' AND ouw.time_start < ' + endtime + ' AND (au.shopid in (' + jsonshop + ') OR au.departmentid = ' + departmentid + ' ) AND au.isdisable = 0 ) as A ON au.id = A.id WHERE ( au.shopid in (' + jsonshop + ') OR au.departmentid = ' + departmentid + ' ) AND au.isdisable = 0 ');
        } else {
          info = await this.model().query(' SELECT au.id,IFNULL(A.type,9) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe,au.isdisable FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + starttime + ' AND ouw.time_start < ' + endtime + ' AND au.shopid in (' + jsonshop + ') AND au.isdisable = 0 ) as A ON au.id = A.id WHERE au.shopid in (' + jsonshop + ') AND au.isdisable = 0 ');
        }

        break;
      case '1':
        info = await this.model().query(' SELECT au.id,IFNULL(A.type,9) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe,au.isdisable FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + starttime + ' AND ouw.time_start < ' + endtime + ' AND au.shopid = ' + jsonshop + ' AND au.isdisable = 0 ) as A ON au.id = A.id WHERE au.shopid = ' + jsonshop + ' AND au.isdisable = 0 ');
        break;
      case '2':
        info = await this.model().query(' SELECT au.id,IFNULL(A.type,9) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe,au.isdisable FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + starttime + ' AND ouw.time_start < ' + endtime + ' AND au.bumenid = ' + jsonshop + ' AND au.isdisable = 0 ) as A ON au.id = A.id WHERE au.bumenid = ' + jsonshop + ' AND au.isdisable = 0 ');
        break;
      case '3':
        info = await this.model().query(' SELECT au.id,IFNULL(A.type,9) as type,au.name,au.headurl,au.sex,IFNULL(A.time_start,0) as time_start,IFNULL(A.time_end,0) as time_end,IFNULL(A.shenhe,0) as shenhe,au.isdisable FROM `think_or_app_user` au LEFT JOIN (SELECT au.id,ouw.type,ouw.shenhe,time_start,time_end FROM `think_or_app_user` au LEFT JOIN `think_or_user_work` ouw ON au.id = ouw.userid WHERE ouw.time_start >= ' + starttime + ' AND ouw.time_start < ' + endtime + ' AND au.xiaozuid = ' + jsonshop + ' AND au.isdisable = 0 ) as A ON au.id = A.id WHERE au.xiaozuid = ' + jsonshop + ' AND au.isdisable = 0 ');
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }

  // 获取今日出单员工列表
  async getsaleusertodayAction() {
    const jsonshop = this.get('jsonshop');
    const departmentid = this.get('departmentid');
    const floor = this.get('floor');
    // 获取今日开始的时间戳
    const time_start = Math.round(new Date(new Date().toDateString()).getTime() / 1000);
    // 今天结束时间
    const time_end = time_start + 86400;
    let info;

    switch (floor) {
      case '0':
        info = await this.model().query('SELECT au.id,au.name,au.headurl,au.sex,IFNULL(A.num,0) as num FROM `think_or_app_user` au LEFT JOIN (SELECT osy.userid,IFNULL(count(osy.id),0) as num FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + time_start + ' AND osy.addtime < ' + time_end + ' AND (au.shopid IN (' + jsonshop + ') OR au.departmentid = ' + departmentid + ') AND au.isdisable = 0  GROUP BY osy.userid) as A ON au.id = A.userid WHERE au.isdisable = 0 AND (au.shopid in (' + jsonshop + ') OR au.departmentid = ' + departmentid + ') ORDER BY A.num DESC ');
        break;
      case '1':
        info = await this.model().query('SELECT au.id,au.name,au.headurl,au.sex,IFNULL(A.num,0) as num FROM `think_or_app_user` au LEFT JOIN (SELECT userid,IFNULL(count(id),0) as num FROM `think_or_shop_yejiticheng` WHERE addtime >= ' + time_start + ' AND addtime < ' + time_end + ' AND shopid = ' + jsonshop + ' GROUP BY userid) as A ON au.id = A.userid WHERE au.shopid = ' + jsonshop + ' AND au.isdisable = 0 ORDER BY A.num DESC ');
        break;
      case '2':
        info = await this.model().query('SELECT au.id,au.name,au.headurl,au.sex,IFNULL(A.num,0) as num FROM `think_or_app_user` au LEFT JOIN (SELECT userid,IFNULL(count(id),0) as num FROM `think_or_shop_yejiticheng` WHERE addtime >= ' + time_start + ' AND addtime < ' + time_end + ' AND bumenid= ' + jsonshop + ' GROUP BY userid) as A ON au.id = A.userid WHERE au.bumenid = ' + jsonshop + ' AND au.isdisable = 0 ORDER BY A.num DESC ');
        break;
      case '3':
        info = await this.model().query('SELECT au.id,au.name,au.headurl,au.sex,IFNULL(A.num,0) as num FROM `think_or_app_user` au LEFT JOIN (SELECT userid,IFNULL(count(id),0) as num FROM `think_or_shop_yejiticheng` WHERE addtime >= ' + time_start + ' AND addtime < ' + time_end + ' AND xiaozuid= ' + jsonshop + ' GROUP BY userid) as A ON au.id = A.userid WHERE au.xiaozuid = ' + jsonshop + ' AND au.isdisable = 0 ORDER BY A.num DESC ');
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 获取今日已服务的客户
  async getguestfuwutodayAction() {
    const jsonshop = this.get('departmentid');
    const floor = this.get('floor');
    const ismanage = this.get('ismanage');
    const user_id = this.get('user_id');
    // 获取今日开始的时间戳
    const starttime = Math.round(new Date(new Date().toDateString()).getTime() / 1000);
    // 今天结束时间
    const endtime = starttime + 86400;
    let info;

    switch (floor) {
      case '0':
      case '1':
        info = await this.model().query(' SELECT og.name,og.id,count(og.id) as num,og.headurl,og.sex,og.arrivetime FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_department` od ON og.departmentid = od.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND au.shopid IN (' + jsonshop + ') GROUP BY osy.guestid  ');
        break;
      case '2':
        info = await this.model().query(' SELECT og.name,og.id,count(og.id) as num,og.headurl,og.sex,og.arrivetime FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_department` od ON og.departmentid = od.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND au.bumenid = ' + jsonshop + ' GROUP BY osy.guestid ');
        break;
      case '3':
        if (ismanage == 1) {
          info = await this.model().query(' SELECT og.name,og.id,count(og.id) as num,og.headurl,og.sex,og.arrivetime FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_department` od ON og.departmentid = od.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND au.xiaozuid = ' + jsonshop + ' GROUP BY osy.guestid ');
        } else if (ismanage == 0) {
          info = await this.model().query('SELECT og.name,og.id,count(og.id) as num,og.headurl,og.sex,og.arrivetime FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id LEFT JOIN `think_or_guest` og ON osy.guestid = og.id LEFT JOIN `think_or_department` od ON og.departmentid = od.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND au.id = ' + user_id + ' GROUP BY osy.guestid ');
        }
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 获取几日销售业绩分类
  async getdongtaixianjintodayAction() {
    const jsonshop = this.get('departmentid');
    const floor = this.get('floor');
    const ismanage = this.get('ismanage');
    const user_id = this.get('user_id');
    const starttime = this.get('start_time_js');
    const endtime = this.get('end_time_js');
    let info;
    switch (floor) {
      case '0':
      case '1':
        info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.xianjinyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND osy.xianjinyeji <> 0 AND au.shopid IN (' + jsonshop + ') GROUP BY osy.userid ');
        break;
      case '2':
        info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.xianjinyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + '  AND osy.xianjinyeji <> 0 AND au.bumenid = ' + jsonshop + ' GROUP BY osy.userid ');
        break;
      case '3':
        if (ismanage == 1) {
          info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.xianjinyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + '  AND osy.xianjinyeji <> 0 AND au.xiaozuid = ' + jsonshop + ' GROUP BY osy.userid ');
        }
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 获取几日卡耗业绩分类
  async getdongtaixiaohaotodayAction() {
    const jsonshop = this.get('departmentid');
    const floor = this.get('floor');
    const ismanage = this.get('ismanage');
    const user_id = this.get('user_id');
    const starttime = this.get('start_time_js');
    const endtime = this.get('end_time_js');
    let info;
    switch (floor) {
      case '0':
      case '1':
        info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.kahaoyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + ' AND osy.kahaoyeji <> 0 AND au.shopid IN (' + jsonshop + ') GROUP BY osy.userid ');
        break;
      case '2':
        info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.kahaoyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + '  AND osy.kahaoyeji <> 0 AND au.bumenid = ' + jsonshop + ' GROUP BY osy.userid ');
        break;
      case '3':
        if (ismanage == 1) {
          info = await this.model().query(' SELECT au.id,au.name,au.headurl,au.sex,IFNULL(sum(osy.kahaoyeji),0) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_app_user` au ON osy.userid = au.id WHERE osy.addtime >= ' + starttime + ' AND osy.addtime < ' + endtime + '  AND osy.kahaoyeji <> 0 AND au.xiaozuid = ' + jsonshop + ' GROUP BY osy.userid ');
        }
        break;
    }

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 获取当前用户的现金业绩组成
  async getuserxianjinlistAction() {
    const user_id = this.get('user_id');
    const start_time = this.get('start_time_js');
    const end_time = this.get('end_time_js');
    const info = await this.model().query(' SELECT og.id,og.name,og.headurl,og.sex,sum(osy.xianjinyeji) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_guest` og ON osy.guestid = og.id WHERE osy.userid = ' + user_id + ' AND osy.addtime >= ' + start_time + ' AND osy.addtime < ' + end_time + ' AND osy.xianjinyeji <> 0 GROUP BY osy.guestid ');
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查询成功...', 'data': info });
  }
  // 获取当前用户的现金业绩组成
  async getuserkahaolistAction() {
    const user_id = this.get('user_id');
    const start_time = this.get('start_time_js');
    const end_time = this.get('end_time_js');
    const info = await this.model().query(' SELECT og.id,og.name,og.headurl,og.sex,sum(osy.kahaoyeji) as feat FROM `think_or_shop_yejiticheng` osy LEFT JOIN `think_or_guest` og ON osy.guestid = og.id WHERE osy.userid = ' + user_id + ' AND osy.addtime >= ' + start_time + ' AND osy.addtime < ' + end_time + ' AND osy.kahaoyeji <> 0 GROUP BY osy.guestid ');
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查询成功...', 'data': info });
  }
  // 检查该员工当日是否有报表
  async getthisuserdaysheetAction() {
    const user_id = this.post('user_id');
    const start_time = this.post('start_time_js');
    const end_time = this.post('end_time_js');
    const info = await this.model().query(' SELECT oud.id,au.name,oud.addtime,au.headurl FROM `think_or_user_daysheet` oud LEFT JOIN `think_or_app_user` au ON oud.userid = au.id WHERE oud.addtime > ' + start_time + ' AND oud.addtime < ' + end_time + ' AND userid = ' + user_id + '  ');

    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '没有数据...', 'data': '' }) : this.json({ 'code': '100', 'msg': '查询成功...', 'data': info });
  }
  // 完成某一个日程
  async postupdataonedayAction() {
    const id = this.post('id');
    const info = await this.model('or_user_manage_day').where({ id: id }).update({ type: 2 });
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '更新失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }
  // 把某一个日程设置为未完成
  async postrestonedayAction() {
    const id = this.post('id');
    const info = await this.model('or_user_manage_day').where({ id: id }).update({ type: 1 });
    return think.isEmpty(info) ? this.json({ 'code': '200', 'msg': '更新失败...', 'data': '' }) : this.json({ 'code': '100', 'msg': 'success...', 'data': info });
  }

  // bottom
};
